home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / OT Code Resource / OTGetDefaultEthernetAddress(SC) / OTGetDefaultEthernetAddress.c next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  3.3 KB  |  135 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:                OTGetDefaultEthernetAddress.c
  3.  
  4.     Contains:        Demo of accessing OT from Symantec C for MPW 68K code resource.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12. */
  13.  
  14. #include <OpenTransport.h>
  15. #include <OpenTptLinks.h>
  16. #include <LibraryManagerUtilities.h>
  17. #include <LibraryManager.h>
  18. #include <HyperXCMD.h>
  19. #include <TextUtils.h>
  20. #include <Strings.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23.  
  24. // Prototypes
  25.  
  26. static void TrueMain(XCmdPtr xpb);
  27.  
  28. // Standard XCMD main entry point
  29.  
  30. pascal void MAIN(XCmdPtr xpb)
  31. {
  32.     OSStatus err;
  33.     GlobalWorld old_world;
  34.  
  35.     // Debugger();
  36.     old_world = GetCurrentGlobalWorld();
  37.     err = InitCodeResource();
  38.     if (err != noErr) {
  39.         // No point using a DebugStr because the string would A5 relative
  40.         //  and this failure indicates that A5 could not be setup!
  41.         Debugger();
  42.     } else {
  43.         TrueMain(xpb);
  44.         
  45.         (void) SetCurrentGlobalWorld(old_world);
  46.         FreeGlobalWorld();
  47.     };    
  48.     // Debugger();
  49. }
  50.  
  51. // Generic OT stuff
  52.  
  53. typedef struct {
  54.     char bytes[6];
  55. } EnetAddress, *EnetAddressPtr;
  56.  
  57. typedef struct T8022Address T8022Address;
  58.  
  59. static T8022Address simple_addr = {
  60.     AF_8022,                                                            // OTAddressType for 802 provider
  61.     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},    // hardware address, use zeros for bind requests
  62.     0x8888,                                                                // SAP / protocol type, value > 1500 implies ethernet protocol with no SNAP
  63.   {0x00,0x00,0x00,0x00,0x00}
  64. };
  65.  
  66. static OSStatus GetDefaultEthernetAddress(EnetAddressPtr def_enet_addr)
  67. {
  68.   EndpointRef ep;
  69.   OSStatus    err;
  70.   TBind       bind_request;
  71.   TBind       bind_result;
  72.   T8022Address  theReturnAddr;
  73.  
  74.   ep = OTOpenEndpoint(OTCreateConfiguration(kEnetName), 0, 0, &err);
  75.   if (err == kOTNoError) {
  76.     // finish bind request
  77.     bind_request.addr.buf = (UInt8 *) &simple_addr;
  78.     bind_request.addr.len = k8022BasicAddressLength;  // family type + Ethernet + type field
  79.     bind_request.addr.maxlen = 0;     
  80.     bind_request.qlen = 0;
  81.     // setup bind result
  82.     bind_result.addr.buf = (UInt8 *) &theReturnAddr;
  83.     bind_result.addr.len = 0;
  84.     bind_result.addr.maxlen = sizeof(theReturnAddr);
  85.     bind_result.qlen = 0;
  86.     // call bind
  87.     err = OTBind(ep, &bind_request, &bind_result);
  88.     if (err == noErr) {
  89.             BlockMove((Ptr) theReturnAddr.fHWAddr, (Ptr) def_enet_addr->bytes, 6);
  90.       // clean up
  91.       err = OTUnbind(ep);
  92.     }
  93.     err = OTCloseProvider(ep);
  94.   }
  95.   return (err);
  96. }
  97.  
  98. static void TrueMain(XCmdPtr xpb)
  99.     // Extra procedure wrapper to avoid horrible register caching problem
  100.     //  whereby the value of result was held in register A3 which was
  101.     //  setup from an A5 relative offset before our A5 had been set up
  102.     //  by InitCodeResource.  Urgh!
  103. {
  104.     OSStatus err;
  105.     EnetAddress def_enet_addr;
  106.     char result[256];
  107.     char tmpstr[3];
  108.     short i;
  109.     
  110.     err = InitOpenTransport();
  111.     if (err == noErr) {
  112.         err = GetDefaultEthernetAddress(&def_enet_addr);
  113.         if (err == noErr) {
  114.             result[0] = 0;
  115.             for (i = 0; i < 6; i++) {
  116.                 tmpstr[0] = "0123456789ABCDEF"[(def_enet_addr.bytes[i] >> 4) & 0x0F];
  117.                 tmpstr[1] = "0123456789ABCDEF"[def_enet_addr.bytes[i] & 0x0F];
  118.                 tmpstr[2] = 0;
  119.                 strcat(result, (char *) tmpstr);
  120.                 if (i < 5) {
  121.                     strcat(result, ":");
  122.                 };
  123.             };
  124.         };
  125.         
  126.         CloseOpenTransport();
  127.     };
  128.     
  129.     if (err != noErr) {
  130.         strcpy(result, "Error");
  131.     };
  132.  
  133.     (void) PtrToHand((Ptr) result, &xpb->returnValue, strlen(result) + 1);
  134. }
  135.